/* 
 * Created on 2006-7-21
 * Last modified on 2006-7-21
 * Powered by YeQiangWei.com
 */
package com.yeqiangwei.net;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.log4j.Logger;

import com.yeqiangwei.util.StringHelper;

public class HttpWrapper {
	
	private final static Logger logger = Logger.getLogger(HttpWrapper.class.getName());
			
	private static HttpClient client = new HttpClient();
	
	public static byte[] getBytes(String url){
		GetMethod getMethod = new GetMethod(url);
		//getMethod.getParams().setContentCharset("UTF-8");
		getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler());
		byte[] responseBody = null;
		try {
			//getMethod.addRequestHeader("Content-Type","text/html;charset=utf-8");
			//执行getMethod   
			int statusCode = client.executeMethod(getMethod);
			if (statusCode != HttpStatus.SC_OK) {
				logger.error("Method failed: "  + getMethod.getStatusLine());           
			}            
			//读取内容          
			responseBody = getMethod.getResponseBody();
			getMethod.getResponseBodyAsStream();
			//System.out.println(getMethod.getResponseCharSet());;
		} catch (HttpException e) {
			logger.error("发生致命的异常，可能是协议不对或者返回的内容有问题");
			logger.error(e.toString());
		} catch (IOException e) {
			logger.error("发生网络异常");
			logger.error(e.toString());
		}finally {            
			//释放连接            
			getMethod.releaseConnection();
			getMethod = null;
		}
		return responseBody;
	}
	
	public static InputStream getStream(String url){
		GetMethod getMethod = new GetMethod(url);
		getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler());
		InputStream ins = null;
		try {
			//getMethod.addRequestHeader("Content-Type","text/html;charset=utf-8");
			//执行getMethod
			String referer = url.replace("http://","");
			referer = StringHelper.substring(referer,0,referer.indexOf("/"),"");
			getMethod.setRequestHeader("referer",referer);
			int statusCode = client.executeMethod(getMethod);
			if (statusCode != HttpStatus.SC_OK) {
				logger.error("Method failed: "  + getMethod.getStatusLine());           
			}            
			//读取内容          
			ins = getMethod.getResponseBodyAsStream();
			//System.out.println(getMethod.getResponseCharSet());;
		} catch (HttpException e) {
			logger.error("发生致命的异常，可能是协议不对或者返回的内容有问题");
			logger.error(e.toString());
		} catch (IOException e) {
			logger.error("发生网络异常");
			logger.error(e.toString());
		}finally {            
			//释放连接      
			getMethod.releaseConnection();
			getMethod = null;
		}
		return ins;
	}
	
	
	
	
	
	
	
	
	
	/*
	public static void main(String args[]) throws UnsupportedEncodingException{
		String url = "http://www.1ting.com";
		String s = new String(Http.getMethod(url),"gb2312");
		//s = Gb.toUTF8(s);
		//System.out.println(new String(s.getBytes("ISO8859-1"),"UTF-8"));
		//s = new String(s.getBytes());
		System.out.println(s);
	}
	*/
	
}














